home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- /* function prototype */
- int getvalue(char *s, char** name, char** version);
-
- main()
- {
- char* server_protocol = getenv("SERVER_PROTOCOL");
- char *name, *versionStr;
- float versionNum;
-
- /* output html MIME type */
- printf("Content-type: text/html\n\n");
-
- printf("<HTML>\n");
- printf("<HEAD><TITLE>CGI Script How-to: Test Script</TITLE></HEAD>\n");
- printf("<BODY>\n");
-
- printf("<H1>CGI Script How-to determine the protocol being used by the server</H1>\n");
-
- /* If name/version strings have been extracted then getvalue returns 0
- * otherwise the value is NULL or in the wrong format.
- */
-
- if (getvalue(server_protocol, &name, &versionStr) == 0)
- {
- /* Use the HTTP information here and convert
- * the revision string to a floating-point number
- */
- versionNum = atof(versionStr);
-
- /* Test the version number: greater, equal, or less than 1.0 */
-
- if (versionNum > 1.0)
- {
- printf("Your server is using a new HTTP protocol\n");
- }
- else if (versionNum == 1.0)
- {
- printf("Your server is using the current HTTP protocol\n");
- }
- else if (versionNum > 0.0)
- {
- printf("Your server is using the old HTTP protocol\n");
- }
- else
- {
- /* version is zero or not even a number */
- printf("Server protocol %s/%s is unknown\n", name, versionStr);
- }
- }
- else
- {
- /* value is NULL or stored in a non-standard format */
- printf("Server protocol is unknown\n");
- }
-
- printf("</BODY></HTML>\n\n");
- exit(0);
- }
-
-
- /*
- * function getvalue()
- *
- * Parses an input string (s) in the form name/version, extracts both the name
- * and version elements and stores these in two output string variables (name,
- * version).
- *
- * Returns: 0 if name/version values extracted from target string,
- * -1 if values not present
- */
-
- int getvalue(char *s, char** name, char** version)
- {
- char *p;
- if (s == NULL || *s == '/')
- {
- return -1; /* null string or no name field */
- }
-
- p = strchr(s, '/'); /* Locate the slash (/) in the string */
- if (p == 0)
- {
- return -1; /* '/' character not found */
- }
-
- *name = malloc(p-s+1);
- if (*name == NULL)
- {
- return -1; /* malloc failed */
- }
- strncpy(*name, s, p-s);
- (*name)[p-s] = '\0'; /* terminate the string */
-
- *version = malloc(strlen(p));
- if (*version == NULL)
- {
- return -1; /* malloc failed */
- }
- strcpy(*version, p+1);
-
- return 0; /* okay, value set */
- }
-
- /*
- * end of testprot.c
- */
-